package test;

import java.io.IOException;

import org.apache.ibatis.session.SqlSession;
import org.junit.Test;

import javastudy1.Teacher;
import javastudy1.CourseMapper;
import javastudy1.Course;
import javastudy1.TeacherMapper;
import utility.MyBatisUtils;

public class MyTest1 {
	
//插入数据	
	@Test
	public void testAdd() throws IOException {
		SqlSession session=MyBatisUtils.openSession();
		TeacherMapper teacherMapper=session.getMapper(TeacherMapper.class);
		Teacher teacher=new Teacher();
		teacher.setName("wang");
		teacher.setSex("男");
		teacher.setPhone("11111111");
		teacherMapper.add(teacher);

		CourseMapper courseMapper=session.getMapper(CourseMapper.class);
		Course course=new Course();
		course.setName("语文");
		course.setType("考试");
		course.setHours(50);
		course.setTeacher(teacher);
		courseMapper.add(course);
		
		course=new Course();
		course.setName("数学");
		course.setType("考试");
		course.setHours(60);
		course.setTeacher(teacher);
		courseMapper.add(course);
		
		session.commit();
		session.close();
	}
	
//查询数据	
	@Test
	public void testSelect() throws IOException {
		SqlSession session=MyBatisUtils.openSession();
//		TeacherMapper teacherMapper=session.getMapper(TeacherMapper.class);
//		Teacher teacher=teacherMapper.get(2);
//		System.out.println(teacher);
		
		CourseMapper courseMapper=session.getMapper(CourseMapper.class);
		Course course=courseMapper.get(1);
		System.out.println(course);
		session.close();
		
	}
	
//删除数据
	@Test
	public void testDelete() throws IOException {
		SqlSession session=MyBatisUtils.openSession();
//		TeacherMapper teacherMapper=session.getMapper(TeacherMapper.class);
//		teacherMapper.delete(3);
		
		CourseMapper courseMapper=session.getMapper(CourseMapper.class);
		courseMapper.delete(3);
		session.commit();
		session.close();
	}
	
	@Test
	public void testUpdate() throws IOException {
		SqlSession session=MyBatisUtils.openSession();
//		TeacherMapper teacherMapper=session.getMapper(TeacherMapper.class);
//		Teacher teacher=new Teacher();
//		teacher.setId(2);
//		teacher.setSex("女");
//		teacherMapper.update(teacher);
		
		CourseMapper courseMapper=session.getMapper(CourseMapper.class);
		Course course=new Course();
		course.setId(1);
		course.setHours(60);
		courseMapper.update(course);
		session.commit();
		session.close();
	}
}
